﻿using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IRepository.WareHouse;
using ProTrack.IServices.SalesOrder;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.SalesOrder
{
    public class OrderSalesExWarehouseServices: IOrderSalesExWarehouseServices
    {
        public IOrderSalesExWarehouseRepository Or { get; }
        public IBaseRepository Db { get; }
        public OrderSalesExWarehouseServices(IOrderSalesExWarehouseRepository or, IBaseRepository db)
        {
            Or = or;
            Db = db;
        }

        public async Task<bool> AddOrderSalesExWareHouse(OrderSalesExWarehouse model, List<OrderSalesExWarehouseItem> list)
        {
            var result = await Or.AddOrderSalesExWareHouse(model,list);
            return result;
        }

        public async Task<bool> UpdateOrderSalesExWareHuse(OrderSalesExWarehouse model, List<OrderSalesExWarehouseItem> list)
        {
            var result = await Or.UpdateOrderSalesExWareHuse(model, list);
            return result;
        }

        public async Task<IEnumerable<OrderSalesExWarehouse>> GetAllAsync()
        {
            string sql = "select * from OrderSalesExWarehouse";
            var list = await Db.QueryAllAsync<OrderSalesExWarehouse>(sql);
            return list;
        }

        public async Task<IEnumerable<OrderSalesExWarehouse>> GetAsync(int PageIndex, int PageSize, string Number,int Status)
        {
            string sql = "select * from OrderSalesExWarehouse where 1=1";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (Status!=-1)
            {
                sql += " and Status =@Status ";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";
            var list = await Db.QueryAsync<OrderSalesExWarehouse>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", Status });
            return list;
        }

        public async Task<int> GetCountAsync(string Number,int Status)
        {
            string sql = "select count(*) from OrderSalesExWarehouse where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (Status != -1)
            {
                sql += " and Status =@Status ";
            }

            var count = await Db.GetRecoreCountAsync(sql, new { Number = $"%{Number}%", Status });
            return count;
        }

        public async Task<IEnumerable<OrderSalesExWarehouseItemDto>> GetItem(string WarehouseId,string OrderSalesExWarehouseId)
        {
            var list = await Db.QueryAsync<OrderSalesExWarehouseItemDto>(@"select ord.*,it.ItemCode,it.UnitOfMeasure,it.Spec,it.ItemName,(isk.StockCount-isk.SalesCount) as CurrCount from OrderSalesExWarehouseItem ord
            inner join Item it on it.Id = ord.ItemId
            inner join OrderSalesExWarehouse o on o.Id=ord.OrderSalesExWarehouseId
            inner join ItemStock isk on isk.ItemId = it.Id and isk.WarehouseId=@WarehouseId 
            where ord.OrderSalesExWarehouseId =@OrderSalesExWarehouseId", new { WarehouseId, OrderSalesExWarehouseId,  });
            return list;
        }

        public async Task<bool> DeleteOrderSalesExWareHuse(int[] Ids)
        {

            var result = await Or.DeleteOrderSalesExWareHuse(Ids);
            return result;
        }

        public async Task<bool> DeleteOrderSalesExWareHuse(int Id)
        {
            var result = await Or.DeleteOrderSalesExWareHuse(Id);
            return result;
        }

        public async Task<bool> UpdatStatus(long Id, int Status)
        {
            var sql = "update OrderSalesExWarehouse set Status=@Status where Id =@Id";

            var result = await Db.UpdateAsync(sql,new {Id,Status });

            return result;
        }
    }
}
